from typing import Callable

from openpyxl.cell.cell import Cell

from core.excel_file import ExcelFile


class ExcelData(ExcelFile):

    __slots__ = [
        'sheet',
        'values',
        'data_row_number'
    ]

    def __init__(self, file: str, sheet_name: str, data_row_number: int = 1):
        super().__init__(file)
        self.sheet = super().get_sheet(sheet_name)
        self.__print_excel_file_info(file, sheet_name)
        self.values = list(self.sheet.values)
        self.data_row_number = data_row_number

    def __print_excel_file_info(self, filename: str, sheet_name: str) -> None:
        print('Excel数据文件<%s>解析完成' % filename)
        print('Sheet<%s>共有数据 %d 行 %d 列' % (sheet_name, self.sheet.max_row, self.sheet.max_column))

    @property
    def data(self):
        return self.values[self.data_row_number - 1:self.sheet.max_row]

    def get_cell_data(self, cell_dimension: str) -> Cell:
        return self.sheet[cell_dimension]

    def find_row_data(self, callback: Callable[[tuple], bool]) -> tuple or None:
        for t in self.data:
            if callback(t):
                return t
